<template>
  <div>
    <el-dialog
      v-loading.lock="loading"
      class="base-dialog system-user-dialog"
      :custom-class="'panna-dialog_big'"
      :append-to-body="true"
      :title="dataList.PlanDetail"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
    >
      <DeviceCalibrationPlanView
        :id="id"
        ref="DeviceCalibrationPlanView"
      ></DeviceCalibrationPlanView>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button type="primary" @click="Export">{{
          dataList.Export
        }}</el-button>
        <el-button @click="cancel">{{ btn.Cancel }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DeviceCalibrationPlanView from "./DeviceCalibrationPlanView.vue";
import { get } from "../../../api/DeviceCalibrationPlan.js";
import { formatTime } from "../../../utils/time";
import { export_json_to_excel } from "@/vendor/Export2Excel";
export default {
  name: "DeviceCalibrationPlanViewDialog",
  components: {
    DeviceCalibrationPlanView,
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formatTime,
      listData: [],
      innerDialogFormVisible: false,
      loading: false,
      from: {
        num: "",
        name: "",
        preparedName: "",
        preparedDate: "",
        remark: "",
      },
      // tHeader: [
      //   "设备编号",
      //   "设备名称",
      //   "型号规格",
      //   "计划检定/校准日期",
      //   "周期",
      //   "责任人",
      //   "责任部门",
      //   "是否校准",
      // ],
      filterVal: [
        "deviceNum",
        "deviceName",
        "specifications",
        "calibrationPlanDate",
        "cycle",
        "personResponsibleName",
        "departName",
        "isCalibrationName",
      ],
    };
  },
  computed: {
    btn() {
      return this.$t("ModuleDevice.buttons");
    },
    dataList() {
      return this.$t("ModuleDevice.Device");
    },
  },
  watch: {
    dialogFormVisible(val) {
      this.innerDialogFormVisible = val;
    },
    innerDialogFormVisible(val) {
      this.$emit("update:dialogFormVisible", val);
    },
  },
  created() {
    this.innerDialogFormVisible = this.dialogFormVisible;
    this.getList();
  },
  methods: {
    getList() {
      get({ id: this.id }).then((res) => {
        this.from = res.data;
        this.listData = res.data.deviceCalibrationPlanInfos;
      });
    },
    export_json_to_excel,
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          return v[j];
        })
      );
    },
    // 导出
    Export() {
      var list = [];
      this.listData = this.$refs.DeviceCalibrationPlanView.listData;
      for (let i = 0; i < this.listData.length; i++) {
        var data = this.listData[i];
        list.push({
          deviceNum: data.deviceNum,
          deviceName: data.deviceName,
          specifications: data.specifications,
          calibrationPlanDate: this.formatTime(data.calibrationPlanDate),
          cycle: data.cycle + data.cycleName,
          personResponsibleName: data.personResponsibleName,
          departName: data.departName,
          isCalibrationName: data.isCalibrationName,
        });
      }

      var datas = this.formatJson(this.filterVal, list);

      const tHeader = [
        this.dataList.DeviceNumber,
        this.dataList.DeviceName,
        this.dataList.DeviceSpecifications,
        this.dataList.CalibrationPlanDate,
        this.dataList.Week,
        this.dataList.DevicePersonResponsibleId,
        this.dataList.Devicedepart,
        this.dataList.IsCalibrationName,
      ];
      export_json_to_excel({
        header: tHeader, // 表头
        data: datas, // 数据
        filename: this.dataList.PlanCheck, // 导出文件名
        autoWidth: true, // 导出宽度自动
        bookType: "xlsx", // 导出后缀
      });
    },
    cancel() {
      this.innerDialogFormVisible = false;
    },
    closeDialog() {
      this.cancel();
    },
  },
};
</script>
<style scoped>
table {
  border-collapse: collapse; /* 合并边框，使其一致 */
  width: 100%;
}
table,
th,
td {
  border: 1px solid gainsboro; /* 设置边框的样式 */
  padding: 8px; /* 可选：设置单元格的内边距 */
}
td {
  text-align: center; /* 水平居中对齐 */
  vertical-align: middle; /* 垂直居中对齐 */
}
#styled-table2 tr:first-child {
  background-color: ghostwhite;
}

#styled-table td:nth-child(1),
#styled-table td:nth-child(3) {
  background-color: ghostwhite;
}
</style>
